package com.qinggeng.project.system.service.impl;

import com.qinggeng.common.enums.UserConsumeEnum;
import com.qinggeng.common.utils.DateUtils;
import com.qinggeng.project.system.domain.UserConsume;
import com.qinggeng.project.system.mapper.UserConsumeMapper;
import com.qinggeng.project.system.service.IUserConsumeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * 用户消费Service业务层处理
 *
 * @author qinggeng
 * @date 2020-06-30
 */
@Slf4j
@Service
public class UserConsumeServiceImpl implements IUserConsumeService
{
    @Resource
    private UserConsumeMapper userConsumeMapper;

    /**
     * 查询用户消费
     *
     * @param consumeId 用户消费ID
     * @return 用户消费
     */
    @Override
    public UserConsume selectUserConsumeById(Long consumeId)
    {
        return userConsumeMapper.selectUserConsumeById(consumeId);
    }

    /**
     * 查询用户消费列表
     *
     * @param userConsume 用户消费
     * @return 用户消费
     */
    @Override
    public List<UserConsume> selectUserConsumeList(UserConsume userConsume)
    {
        return userConsumeMapper.selectUserConsumeList(userConsume);
    }

    /**
     * 新增用户消费
     *
     * @param userConsume 用户消费
     * @return 结果
     */
    @Override
    public int insertUserConsume(UserConsume userConsume)
    {
        userConsume.setCreateTime(DateUtils.getNowDate());
        return userConsumeMapper.insertUserConsume(userConsume);
    }

    /**
     * 修改用户消费
     *
     * @param userConsume 用户消费
     * @return 结果
     */
    @Override
    public int updateUserConsume(UserConsume userConsume)
    {
        return userConsumeMapper.updateUserConsume(userConsume);
    }

    /**
     * 批量删除用户消费
     *
     * @param consumeIds 需要删除的用户消费ID
     * @return 结果
     */
    @Override
    public int deleteUserConsumeByIds(Long[] consumeIds)
    {
        return userConsumeMapper.deleteUserConsumeByIds(consumeIds);
    }

    /**
     * 删除用户消费信息
     *
     * @param consumeId 用户消费ID
     * @return 结果
     */
    @Override
    public int deleteUserConsumeById(Long consumeId)
    {
        return userConsumeMapper.deleteUserConsumeById(consumeId);
    }

    @Override
    public int addUserConsume(Long userId, BigDecimal price, Long ordersId, UserConsumeEnum enumType,String consumeMark) {
        int rows=0;
        try{
            UserConsume userConsume = new UserConsume();
            userConsume.setConsumeType(enumType.getValue());
            userConsume.setConsumeTitle(enumType.name());
            userConsume.setConsumePrice(price);
            userConsume.setConsumeMark(consumeMark);
            userConsume.setUserId(userId);
            userConsume.setFkId(ordersId);
            rows=insertUserConsume(userConsume);
        }catch (Exception e){
            log.error(e.getMessage());
            System.out.println(e.getMessage());
        }
        return rows;
    }
}
